/**
 * Created by Administrator on 2017/6/21.
 */

function setAutoApprove(bool){
    if(bool){
        $("input[name='autoApprove']").val("true");
    }else{
        var autoApproveScopes = $("#autoApproveScopes").select2("data");
        var autoApproves = [];
        for (var i=0;i<autoApproveScopes.length;i++){
            autoApproves.push(autoApproveScopes[i].text);
        }
        $("input[name='autoApprove']").val(autoApproves.join(","));
    }
}
$(function () {
    activeMenus('客户端管理');
    $("#autoApproveScopes").on("change",function(e){
        if($("input[name='autoApprove']").val()!="true"){
            setAutoApprove(false);
        }
    });
    $("#autoApproveAll").on('ifChecked', function (e) {
        setAutoApprove(true);
    });
    $("#autoApproveAll").on('ifUnchecked', function (e) {
        setAutoApprove(false);
    });

    $("#redirectUris").on('blur',function (e) {
        console.log($(this).val());
        if($.trim($(this).val())!=""){
            $("#registeredRedirectUris").val($(this).val().replace(/[\r\n]/g,","));
        }
    });
    $(".btn-submit").on("click", function (e) {
        bootbox.confirm({
            message: "Do you confirm to submit?",
            buttons: {
                confirm: {
                    label: 'Yes',
                    className: 'btn-success'
                },
                cancel: {
                    label: 'No',
                    className: 'btn-danger'
                }
            },
            callback: function (result) {
                console.log('This was logged in the callback: ' + result);
                if(result){
                    var formJsonData = JSON.stringify($('#clientDetailsForm').serializeObject());
                    var url = $("#clientDetailsForm").attr("action");
                    var method = $("#clientDetailsForm").attr("method");
                    //PUT Mode
                    if($("#clientDetailsForm > input[name='_method']").length>0){
                        url = url + "/" + $("#clientDetailsForm #clientId").data("client-id");
                        method = $("#clientDetailsForm > input[name='_method']").val();
                    }
                    console.log(formJsonData);
                    console.log($("#clientDetailsForm > input[name='_method']").length>0?$("#clientDetailsForm > input[name='_method']").val():$("#clientDetailsForm").attr("method"));
                    $.ajax({
                        method: method,
                        contentType: "application/json; charset=utf-8", //default application/x-www-form-urlencoded; charset=UTF-8
                        url: url,
                        data:formJsonData,
                        success: function(msg,status){
                            console.log( "Data Created Success: " + msg +" -->status:"+status);
                            if(status == "success"){
                                location.href = "clientDetails";
                            }
                        },
                        error: function (e) {
                            console.log(e)
                            console.log("Client already exists");
                        }
                    });
                }
            }
        });
    })

    $(".btn-delete").on("click", function (e) {
        var clientId = $(this).data("client-id");
        console.log("clientId:"+clientId);
        bootbox.confirm({
            title: "删除ClientDetails",
            message: "Do you want to delete the clientDetails?",
            buttons: {
                cancel: {
                    label: '<i class="fa fa-times"></i> Cancel'
                },
                confirm: {
                    label: '<i class="fa fa-check"></i> Confirm'
                }
            },
            callback: function (result) {
                console.log('This was logged in the callback: ' + result);
                if(result){
                    $.ajax({
                        method: "DELETE",
                        url: "clientDetails/"+clientId,
                        success: function(msg){
                            console.log( "Data Deleted: " + msg );
                            location.href = "clientDetails"
                        },
                        error: function (e) {
                            console.log(e)
                            console.log("Client doesn't found");
                        }
                    });
                }
            }
        });

    })
});